﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace AMatysiak.Reflection
{
    public class StringSerializer
    {
        public static string Serialize(object obj)
        {
            MemoryStream memoryStream = new MemoryStream();
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            binaryFormatter.Serialize(memoryStream, obj);
            string str = System.Convert.ToBase64String(memoryStream.ToArray());
            return str;
        }

        public static object Deserialize(string serial)
        {           
            byte[] bytes = System.Convert.FromBase64String(serial);
            MemoryStream memoryStream = new MemoryStream(bytes);
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            object obj = binaryFormatter.Deserialize(memoryStream);
            return obj;
        }
    }
}
